//在 wwechat 环境下模拟 fetch

var NODE_PATH = require('path');

var helperPluginUtils = require("@babel/helper-plugin-utils");

var _helperModuleImports = require("@babel/helper-module-imports");
var _core = require("@babel/core");

var f =  helperPluginUtils.declare((api, options, dirname)=> {
    return {
        name: 'wx-plugin',
        visitor: {
            Identifier(path) {
                const name = path.node.name;

                if('regeneratorRuntime' != name){
                    return;
                }

                var filename = path.hub.file.opts.filename;
                var cwd = path.hub.file.opts.cwd;

                var relativePath = filename.substr(cwd.length, filename.length);
                if(relativePath == '/src/regenerator-runtime.js'
                    || relativePath == '/test/regenerator-runtime.js'
                    || relativePath == '/src/index.js'){
                    return;
                }

                var seps = relativePath.match(new RegExp('\\' + NODE_PATH.sep,  'g'));

                if(seps == null ){
                    return
                }
                var runtime = './';
                for(var n =0;n<seps.length-2;n++){
                    runtime += "../"
                }
                runtime += 'regenerator-runtime';
                // console.log('====')
                path.node.name = 'require(\'' + runtime + '\')';

            },
        },
        pre(file) {

        },
    };
})
module.exports = f;
